I have created a GraphChart project using VB.NET (2010), my project has the following six forms:
- frmChart: This is the main form to choose the type of the chart.
- frmGraph1: Binding the chart to an array from a DataGrid with (DataBindXY) and showing the following chart:
- frmGraph2: Using the DataBindXY method the same as the previous form but by binding the chart to data from a MDB file.
- frmGraph3: Using the DataBindTable method by binding the chart to data from a MDB file.
- frmGraph4: Using the AddXY method by binding the chart to data from a MDB file.
- frmGraph5: Using the DataSource and binding the chart to data from a MDB file with the DataBind method.
About the Code
I begin the code in all the forms with the following procedure:
- Private Sub LoadSchoolData()
-
- MyDataFile = Application.StartupPath & "\DataFile\MySchool.mdb"
- strConnection = "provider=microsoft.jet.oledb.4.0;data source=" _
- & MyDataFile & ";" & "Jet OLEDB:Database Password=" & MyPass & ";"
-
- Try
-
- strSql = "SELECT EducationalYear, StudentGrade, COUNT(*) AS Total FROM StudentData " _
- & "GROUP BY EducationalYear, StudentGrade " _
- & "ORDER BY EducationalYear; "
-
-
- Dim datAdp As OleDbDataAdapter = New OleDbDataAdapter(strSql, strConnection)
- tblStudents = New DataTable
- datAdp.Fill(tblStudents)
-
-
- GridSchool.DataSource = tblStudents
- lblSample.Text = "Number of students from 2011 to 2014:"
- Catch ex As Exception
- MessageBox.Show(ex.ToString())
- End Try
- End Sub
But the statement "strSql" varies from one form to another.
Any form has a procedure to create the "ChartArea", "Series", "Legend" and "Title" of the chart.
The following procedure creates a ChartArea in the form "frmGraph1":
- Private Sub CreateChartArea()
-
- MyChart.ChartAreas.Clear()
-
- MyChartArea = New ChartArea()
-
- MyChart.ChartAreas.Add("MyChartArea")
-
-
- MyChart.BackColor = Color.LightGray
- MyChart.ChartAreas("MyChartArea").BackSecondaryColor = Color.Green
- MyChart.ChartAreas("MyChartArea").BackGradientStyle = GradientStyle.TopBottom
-
- MyChart.ChartAreas("MyChartArea").AxisX.MajorGrid.Enabled = False
-
- MyChart.ChartAreas("MyChartArea").AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash
-
- MyChart.ChartAreas("MyChartArea").AxisX.Maximum = 5
-
- MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.IsEndLabelVisible = False
-
- MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.Angle = 30
-
- MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.Font = New Font("Arial", 10, FontStyle.Bold)
-
- MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.ForeColor = Color.Blue
-
- MyChart.ChartAreas("MyChartArea").AxisY.Title = "Number of students"
-
- MyChart.ChartAreas("MyChartArea").AxisY.TitleFont = New Font("Arial", 10, FontStyle.Bold)
-
- MyChart.ChartAreas("MyChartArea").AxisY.TitleForeColor = Color.Blue
-
-
-
- End Sub
You can read the procedure "DrawGraph" that draws the chart in all the forms.
The following procedure draws the chart in the form "frmGraph4".
- Private Sub DrawGraph()
-
- For Each RowNum As DataRow In tblStudents.Rows
-
- Dim SeriesName As String = RowNum("StudentGrade").ToString()
- MyChart.Series.Add(SeriesName)
-
- MyChart.Series(SeriesName).ChartType = SeriesChartType.Column
-
- MyChart.Series(SeriesName)("DrawingStyle") = "Cylinder"
-
- For COlNum As Integer = 1 To (tblStudents.Columns.Count) - 1
- Dim ColName As String = tblStudents.Columns(COlNum).ColumnName
- Dim YValue As Integer = Convert.ToInt32(RowNum(ColName))
- MyChart.Series(SeriesName).Points.AddXY(ColName, YValue)
- Next COlNum
- Next RowNum
-
- MyChart.Series(0).Color = Color.Blue
- MyChart.Series(1).Color = Color.Yellow
- MyChart.Series(2).Color = Color.Red
- End Sub
SummaryIt is desirable to beautify your data with charts, so I invite you to go to the source file to read the code because I cannot list all the source code for the limited space available. If you have any idea about this code, please tell me. Thanks for the C# Corner team and thanks for all.